﻿Imports System.IO

Public Class frmThemCuaHang
    Event ThemCuaHangThanhCong()
    Private newCuaHang As New tbCuaHang
    Private luuTamCuaHang As New tbCuaHang

    Private Sub frmThemCuaHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
        CtrlCuaHang1.txtMaCuaHang.Focus()
        CtrlCuaHang1.CuaHang = newCuaHang
    End Sub

    Private Sub btnThem_Click(sender As Object, e As EventArgs) Handles btnThem.Click
        CtrlCuaHang1.KryptonLabel1.Focus()
        dt.Refresh(Data.Linq.RefreshMode.OverwriteCurrentValues, dt.tbCuaHangs)
        If newCuaHang.MaCuaHang Is Nothing Or CtrlCuaHang1.txtMaCuaHang.Text = "" Then
            ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Mã cửa hàng không được để trống!", "Thông Báo", _
                                                                     MessageBoxButtons.OK, MessageBoxIcon.Information)
            CtrlCuaHang1.txtMaCuaHang.Focus()
            Exit Sub
        End If

        If dt.tbCuaHangs.Where(Function(s) s.MaCuaHang = CtrlCuaHang1.txtMaCuaHang.Text).Count > 0 Then
            ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Mã cửa hàng đã tồn tại trong hệ thống!", "Thông Báo", _
                                                                     MessageBoxButtons.OK, MessageBoxIcon.Information)
            CtrlCuaHang1.txtMaCuaHang.Focus()
            Exit Sub
        End If

        If newCuaHang.TenCuaHang Is Nothing Or CtrlCuaHang1.txtTenCuaHang.Text = "" Then
            ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Tên cửa hàng không được để trống!", "Thông Báo", _
                                                                     MessageBoxButtons.OK, MessageBoxIcon.Information)
            CtrlCuaHang1.txtTenCuaHang.Focus()
            Exit Sub
        End If

        ' Trùng tên cửa hàng
        If dt.tbCuaHangs.Where(Function(s) s.TenCuaHang = CtrlCuaHang1.txtTenCuaHang.Text).Count > 0 Then
            ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Tên cửa hàng đã tồn tại trong hệ thống!", "Thông Báo", _
                                                                     MessageBoxButtons.OK, MessageBoxIcon.Information)
            CtrlCuaHang1.txtTenCuaHang.Focus()
            Exit Sub
        End If
        Dim ms As New MemoryStream
        CtrlCuaHang1.pctLogo.Image.Save(ms, CtrlCuaHang1.pctLogo.Image.RawFormat)
        Dim arrImage() As Byte = ms.GetBuffer
        ms.Close()

        newCuaHang.LogoTrenBill = arrImage

        dt.tbCuaHangs.InsertOnSubmit(newCuaHang)
        dt.SubmitChanges()
        RaiseEvent ThemCuaHangThanhCong()

        If chkCopyNhanVien.Checked Then
            luuTamCuaHang = newCuaHang
        End If

        newCuaHang = New tbCuaHang
        If chkCopyNhanVien.Checked Then
            newCuaHang.TenCuaHang = luuTamCuaHang.TenCuaHang
            newCuaHang.MaCuaHang = luuTamCuaHang.MaCuaHang
            newCuaHang.NguoiQuanLy = luuTamCuaHang.NguoiQuanLy
            newCuaHang.DiaChiCuaHang = luuTamCuaHang.DiaChiCuaHang
            newCuaHang.SoDienThoai = luuTamCuaHang.SoDienThoai
            newCuaHang.TenCuaHangTrenBill = luuTamCuaHang.TenCuaHangTrenBill
            newCuaHang.ThongTinLienHeTrenBill = luuTamCuaHang.ThongTinLienHeTrenBill
            newCuaHang.LogoTrenBill = luuTamCuaHang.LogoTrenBill
        End If
        CtrlCuaHang1.bsCuaHang.DataSource = newCuaHang
        CtrlCuaHang1.txtMaCuaHang.Focus()
        CtrlCuaHang1.txtMaCuaHang.Select(0, CtrlCuaHang1.txtMaCuaHang.Text.Length)
    End Sub

    Private Sub btnDong_Click(sender As Object, e As EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub frmThemCuaHang_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.F2 Then
            btnThem_Click(Nothing, Nothing)
        ElseIf e.KeyCode = Keys.F3 Then
            Me.Close()
        End If
    End Sub
End Class